package com.dongjiantong.user.serviceImpl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.dongjiantong.mybatis.ServiceMybatis;
import com.dongjiantong.sys.model.SysArea;
import com.dongjiantong.sys.service.SysAreaService;
import com.dongjiantong.user.mapper.OrgUserAreaMapper;
import com.dongjiantong.user.model.UserArea;
import com.dongjiantong.user.model.UserAreaModel;
import com.dongjiantong.user.model.dtos.UserAreaDto;
import com.dongjiantong.user.service.OrgUserAreaService;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author cao
 * @create 2017-11-28 下午1:10
 **/
@Slf4j
@Service(version = "1.0.0")
public class OrgUserAreaServiceImpl extends ServiceMybatis<UserArea> implements OrgUserAreaService {

    @Resource
   private OrgUserAreaMapper orgUserAreaMapper;


    @Override
   public List<UserArea> queryByUserId(long userId)
   {
       Example example = new Example(UserArea.class);

       Example.Criteria criteria= example.createCriteria();
       criteria.andEqualTo("userId",userId);


       return orgUserAreaMapper.selectByExample(example);
   }

    /**
     *  根据用户id查询树形区域菜单
     * @param userId
     * @return
     */
   @Override
   public List<Object> queryLocationArea(long userId)
   {
       List<Object> areaList = Lists.newArrayList();

       //查省

       List<UserAreaDto> pList =  orgUserAreaMapper.queryProvince(userId);
       //市
       List<UserAreaDto> cList =  orgUserAreaMapper.queryCity(userId);
       //区县
       List<UserAreaDto> rList = orgUserAreaMapper.queryRegion(userId);

       //乡镇
       List<UserAreaDto> sList = orgUserAreaMapper.queryStreet(userId);

       //村
       List<UserAreaDto> vList = orgUserAreaMapper.queryVillage(userId);



      if(pList!=null && pList.size()>0)
       {
           for (UserAreaDto p:pList) {




               UserAreaModel province = new UserAreaModel();
               province.setLabel(p.getAreaName());
               province.setValue(p.getProvinceId());

               province.setChildren(Lists.newArrayList());

               //市
               List<UserAreaDto>   cityList = cList.stream().filter(i->i.getProvinceId().equals(p.getProvinceId())).collect(Collectors.toList());

               for(UserAreaDto c:cityList)
               {


                   UserAreaModel city = new UserAreaModel();
                   city.setLabel(c.getAreaName());
                   city.setValue(c.getCityId());
                   city.setChildren(Lists.newArrayList());

                   //县
                   List<UserAreaDto>   regionList = rList.stream().filter(i->i.getProvinceId().equals(c.getProvinceId())
                   && i.getCityId().equals(c.getCityId())
                   ).collect(Collectors.toList());

                   for(UserAreaDto r:regionList) {


                       UserAreaModel region = new UserAreaModel();
                       region.setLabel(r.getAreaName());
                       region.setValue(r.getRegionId());
                       region.setChildren(Lists.newArrayList());

                       //镇
                       List<UserAreaDto>   streetList = sList.stream().filter(i->i.getProvinceId().equals(r.getProvinceId())
                               && i.getCityId().equals(r.getCityId())
                               && i.getRegionId().equals(r.getRegionId())
                       ).collect(Collectors.toList());

                       for(UserAreaDto s:streetList)
                       {

                           UserAreaModel street = new UserAreaModel();
                           street.setLabel(s.getAreaName());
                           street.setValue(s.getStreetId());
                           street.setChildren(Lists.newArrayList());

                           //村

                           List<UserAreaDto>   villageList = vList.stream().filter(i->i.getProvinceId().equals(s.getProvinceId())
                                   && i.getCityId().equals(s.getCityId())
                                   && i.getRegionId().equals(s.getRegionId())
                                   && i.getStreetId().equals(s.getStreetId())
                           ).collect(Collectors.toList());

                           for (UserAreaDto v:villageList) {


                               UserAreaModel village = new UserAreaModel();
                               village.setLabel(v.getAreaName());
                               village.setValue(v.getVillageId());

                               street.getChildren().add(village);

                           }


                           region.getChildren().add(street);
                       }


                       city.getChildren().add(region);

                   }


                   province.getChildren().add(city);
               }




               areaList.add(province);
           }


       }
      return areaList;
   }


}